package com.nttuan.mycalender;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.nttuan.model.dao.UserDao;
import com.nttuan.model.dao.service.UserService;
import com.nttuan.model.entity.User;
import com.nttuan.util.JSONParser;
import com.nttuan.util.LogSupport;
import com.nttuan.util.Network;

/**
 * Created by TuanNT on 12/3/2014.
 */
public class RegisterActivity extends Activity implements View.OnClickListener{
    public static final String KEY_BUNDLE ="1";
    public static final String KEY_UPDATE ="2";

    private EditText name;
    private EditText email;
    private EditText address;
    private EditText phone;
    private EditText facebook;
    private EditText skyper;
    private EditText password;
    private EditText helpCode;
    private Button btnRegister;
    private UserService userService;
    private ProgressBar progressBar;

    private boolean isUpdate;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        userService = UserDao.getInstance(this);

        Bundle bundle = getIntent().getBundleExtra(KEY_BUNDLE);
        if(bundle != null) {
            isUpdate = bundle.getBoolean(KEY_UPDATE);
        }
        else
            isUpdate =false;

        progressBar=(ProgressBar)findViewById(R.id.progressBar);
        name =(EditText)findViewById(R.id.txtRegisterName);
        email =(EditText)findViewById(R.id.txtRegisterEmail);
        address =(EditText)findViewById(R.id.txtRegisterAddress);
        phone =(EditText)findViewById(R.id.txtRegisterPhone);
        facebook =(EditText)findViewById(R.id.txtRegisterFacebook);
        skyper =(EditText)findViewById(R.id.txtRegisterSkyper);
        password =(EditText)findViewById(R.id.txtPassword);
        helpCode =(EditText)findViewById(R.id.txtHelpCode);
        btnRegister =(Button)findViewById(R.id.btnRegister);
        btnRegister.setOnClickListener(this);
    }
    private void lockUI(boolean isLock){
        if(isLock)
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        else
            this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    }
    class Register extends AsyncTask<Void, Void, Void>{
        private User user;
        private boolean success;
        public Register(User user){
            this.user = user;
        }
        @Override
        protected Void doInBackground(Void... voids) {
            if(isUpdate) {
                LogSupport.setKeyHelp(RegisterActivity.this,helpCode.getText().toString());
                if (userService.update(user))
                    success = true;
                else
                    success = false;
                return null;
            }

            if(JSONParser.register(user)) {
                LogSupport.setKeyHelp(RegisterActivity.this,helpCode.getText().toString());
                if (userService.create(user))
                    success = true;
                else
                    success = false;
            }
            else
                success = false;
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            progressBar.setVisibility(View.GONE);
            lockUI(false);
            if(isUpdate) {
                finish();
                return;
            }
            if(success) {
               // Toast.makeText(getBaseContext(), R.string.register_success, Toast.LENGTH_SHORT).show();
                startActivityMain();
            }
            else
                Toast.makeText(getBaseContext(),R.string.register_fail,Toast.LENGTH_SHORT).show();

        }

        @Override
        protected void onPreExecute() {
            progressBar.setVisibility(View.VISIBLE);
            lockUI(true);
        }
    }


    @Override
    public void onClick(View view) {
        if(Network.isConnect(this))
            new Register(getValues()).execute();
        else
            Toast.makeText(getBaseContext(),R.string.not_connect,Toast.LENGTH_SHORT).show();

    }


    @Override
    protected void onResume() {
        super.onResume();
        if(userService.newId() > 1 && isUpdate ==false) {
            startActivityMain();
            finish();
        }else
        {
            if(isUpdate)
                setValues(userService.getMe());
        }
    }

    private void startActivityMain(){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
    private void setValues(User user){

        name.setText(user.getUserName());
        email.setText(user.getEmail());
        address.setText(user.getAddress());
        phone.setText(user.getPhone());
        facebook.setText(user.getFacebook());
        skyper.setText(user.getSkype());
        password.setText(user.getPassword());
        helpCode.setText(LogSupport.getKeyHelp(this));
        btnRegister.setText(getText(R.string.btn_update));
    }
    private User getValues(){
        User user= new User();
        if(isUpdate)
            user = userService.getMe();
        user.setUserName(name.getText().toString());
        user.setEmail(email.getText().toString());
        user.setAddress(address.getText().toString());
        user.setPhone(phone.getText().toString());
        user.setFacebook(facebook.getText().toString());
        user.setSkype(skyper.getText().toString());
        user.setPassword(password.getText().toString());
        user.setActive(true);
        return  user;
    }
}
